#import "WXUtil.h"

#import "WXApi.h"

//extern NSString *WEIXIN_APP_ID;

@implementation ADD_CLASS(WXUtil)

//int WXOnRespType
//int WXOnRespErrCode;
NSString *WEIXIN_APP_ID;
NSString *WXLoginCode;

NSMutableDictionary *dict = nil;

+ (void) ADD_FUN(registerApp):(NSString *)appid {
    WEIXIN_APP_ID = appid;
    dict = [[NSMutableDictionary alloc] init];
    [WXApi registerApp:WEIXIN_APP_ID];
}

+ (bool) ADD_FUN(isWXAppInstalled) {
    return [WXApi isWXAppInstalled];
}

+ (NSString *) ADD_FUN(getWeixinAppId) {
    return WEIXIN_APP_ID;
}

+ (void) ADD_FUN(setWXOnRespResult):(NSString *)k v:(int)v {
    [dict setValue:@(v) forKey:k];
}

+ (bool) ADD_FUN(hasWXOnRespResult):(NSString *)k {
    return [[dict allKeys] containsObject:k];
}

+ (int) ADD_FUN(getWXOnRespResult):(NSString *)k {
    return [dict[k] intValue];
}

+ (void) ADD_FUN(redirectToWeixinLogin) {
    SendAuthReq *req = [[[SendAuthReq alloc] init] autorelease];
    req.scope = @"snsapi_userinfo";
    [WXApi sendReq:req];
}

+ (NSString *) ADD_FUN(getWXLoginCode) {
    return WXLoginCode;
}

+ (void) ADD_FUN(setWXLoginCode):(NSString *)code {
    WXLoginCode = [[NSString alloc] initWithString:code];
}

//+ (void) wxPay:(NSString *)partnerId prepayId:(NSString *) prepayId package:(NSString *)package nonceStr:(NSString *)nonceStr timeStamp:(NSString *)timeStamp sign:(NSString *)sign
//{
//    PayReq *request = [[[PayReq alloc] init] autorelease];
//    request.partnerId = partnerId;
//    request.prepayId= prepayId;
//    request.package = package;
//    request.nonceStr= nonceStr;
//    request.timeStamp= [timeStamp intValue];
//    request.sign= sign;
//    [WXApi sendReq:request];
//}

+ (void) ADD_FUN(shareText):(NSString *)text sceneType:(NSNumber *)sceneType {
    SendMessageToWXReq *req = [[[SendMessageToWXReq alloc] init] autorelease];
    req.text = text;
    req.bText = YES;
    req.scene = [sceneType intValue];
    [WXApi sendReq:req];
}

+ (void) ADD_FUN(shareUrl):(NSString *)url title:(NSString *)title description:(NSString *)description sceneType:(NSNumber *)sceneType {
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = title;
    message.description = description;
    UIImage *appIcon = [UIImage imageNamed:@"res/icon.png"];

    [message setThumbImage:appIcon];

    WXWebpageObject *ext = [WXWebpageObject object];
    ext.webpageUrl = url;

    message.mediaObject = ext;

    SendMessageToWXReq *req = [[[SendMessageToWXReq alloc] init] autorelease];
    req.bText = NO;
    req.message = message;
    req.scene = [sceneType intValue];

    [WXApi sendReq:req];
}

+ (void) ADD_FUN(shareUrlWithIcon):(NSString *)url title:(NSString *)title description:(NSString *)description sceneType:(NSNumber *)sceneType path:(NSString *)path {
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = title;
    message.description = description;
    UIImage *appIcon = [UIImage imageNamed:path];

    [message setThumbImage:appIcon];

    WXWebpageObject *ext = [WXWebpageObject object];
    ext.webpageUrl = url;

    message.mediaObject = ext;

    SendMessageToWXReq *req = [[[SendMessageToWXReq alloc] init] autorelease];
    req.bText = NO;
    req.message = message;
    req.scene = [sceneType intValue];

    [WXApi sendReq:req];
}

+ (void) ADD_FUN(sharePic):(NSString *)filePath imageName:(NSString *)imageName sceneType:(NSNumber *)sceneType {
    WXMediaMessage *message = [WXMediaMessage message];

    WXImageObject *ext = [WXImageObject object];
    ext.imageData = [NSData dataWithContentsOfFile:[[filePath stringByAppendingString:@"/"] stringByAppendingString:imageName]];

    UIImage *image = [UIImage imageWithData:ext.imageData];
    ext.imageData = UIImageJPEGRepresentation(image, 0.6f);

    message.mediaObject = ext;

    CGSize size;
    size.width = 300;
    size.height = (int) (300 / (double) image.size.width * image.size.height);
    UIImage *thunbImage = [self imageCompressWithSimple:image scaledToSize:size];
    [message setThumbImage:thunbImage];

    SendMessageToWXReq *req = [[[SendMessageToWXReq alloc] init] autorelease];
    req.bText = NO;
    req.message = message;
    req.scene = [sceneType intValue];

    [WXApi sendReq:req];
}

+ (UIImage *) ADD_FUN(imageCompressWithSimple):(UIImage *)image scaledToSize:(CGSize)size {
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

@end
